﻿using OpenAuth.Domain.Core;
using OpenAuth.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenAuth.Repository
{
    public class OrganizeRepository : BaseRepository<Organize>, IOrganizeRepository
    {
        /// <summary>
        /// 分页获取类别
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IEnumerable<Organize> LoadOrganizes(int pageindex, int pagesize)
        {
            return Context.Organizes.OrderBy(u => u.Id).Skip((pageindex - 1) * pagesize).Take(pagesize);
        }
        /// <summary>
        /// 通过IDs获取类别
        /// </summary>
        /// <param name="sortIds"></param>
        /// <returns></returns>
        public IEnumerable<Organize> LoadInOrganizes(params Guid[] organizeIds)
        {
            var result = from organizeId in Context.Organizes
                         where organizeIds.Contains(organizeId.Id)
                         select organizeId;
            return result;

        }
        /// <summary>
        /// 获取类别条数
        /// </summary>
        /// <param name="organizeIds"></param>
        /// <returns></returns>
        public int GetOrganizeCntInOrganizes(params Guid[] organizeIds)
        {
            return LoadInOrganizes(organizeIds).Count();
        }
        /// <summary>
        /// 分页获取类别数据
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="organizeIds"></param>
        /// <returns></returns>
        public IEnumerable<Organize> LoadInOrganizes(int pageindex, int pagesize, params Guid[] organizeIds)
        {
            return LoadInOrganizes(organizeIds).OrderBy(u => u.Id).Skip((pageindex - 1) * pagesize).Take(pagesize);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(Guid id)
        {
            Delete(u => u.Id == id);
        }

        /// <summary>
        /// 获取当前节点的所有下级节点ID
        /// </summary>
        public Guid[] GetSubIds(Guid organizeId)
        {
            if (organizeId == Guid.Empty)
            {
                return Find(null).Select(u => u.Id).ToArray();
            }
            var sort = FindSingle(u => u.Id == organizeId);
            var ids = Find(u => u.CascadeId.Contains(sort.CascadeId)).Select(u => u.Id).ToArray();
            return ids;
        }
    }
}
